/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package limousine.report;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author TEComp
 */
@WebServlet(name = "InvoiceReportServlet", urlPatterns = {"/InvoiceReportServlet"})
public class InvoiceReportServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        //File reportFile = new File(getServletConfig().getServletContext().getRealPath("/dialyDispatchReport.jasper"));
        byte[] bytes = null;

        try {
            //bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
            //        new HashMap(), new JREmptyDataSource());
            // bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
            //        new HashMap(), new JREmptyDataSource());
            DataSource dataSource = (DataSource) WebApplicationContextUtils.getWebApplicationContext(
                    this.getServletContext()).getBean("dataSource");
            Connection connection = dataSource.getConnection();
           System.out.println("fromTime["+ request.getParameter("fromTime").split(" ")[0]+"]");
           System.out.println("toTime["+ request.getParameter("toTime").split(" ")[0]+"]");
           System.out.println("customerId["+ request.getParameter("customerId")+"]");
           ///AND A.CREATE_DATE BETWEEN $P{fromDate} AND $P{toDate}
//AND A.CUSTOMER_ID = $P{customerId} 
           ///
           ///
            HashMap jasperParameter = new HashMap();
            
            jasperParameter.put("fromDate", request.getParameter("fromTime").split(" ")[0] );
            jasperParameter.put("toDate", request.getParameter("toTime").split(" ")[0] );
            jasperParameter.put("customerId", request.getParameter("customerId") );
            //jasperReport = JasperCompileManager.compileReport(this.getClass().getResourceAsStream(""));
            
            
            bytes = JasperRunManager.runReportToPdf(
                    //this.getClass().getResourceAsStream("./dialyDispatchReport.jasper"),
                    this.getClass().getResourceAsStream("./invoice.jasper"),
                    jasperParameter, connection);
            connection.close();
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            servletOutputStream.write(bytes, 0, bytes.length);
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch(SQLException ex) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
        } catch (JRException ex) {
            // display stack trace in the browser
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
